<?php

namespace app\common\model;

use think\Model;
use think\Db;

/**
 * 后台管理员用户信息
 * @author root
 *
 */
class User extends Model {

  /**
   * getRoleInfo 获取角色信息
   * @author yq
   */
  public function getRoleInfo($id, $dbname) {

    $result = Db::name($dbname . '_auth_group')->where('id', $id)->find();
    if (empty($result['rules'])) {
      $where = '';
    } else {
      $where = 'id in(' . $result['rules'] . ')';
    }
    $res = Db::name($dbname . '_auth_rule')->field('url')->where($where)->select();
    foreach ($res as $key => $vo) {
      if ('#' != $vo['url']) {
        $result['name'][] = $vo['url'];
      }
    }

    return $result;
  }

  /**
   * 
   * @param unknown $field 查询字段
   * @param unknown $where 条件
   * @param unknown $dbname 主表
   * @param unknown $sub_table 分表
   * @param unknown $order 排序
   * @return array();
   */
  public function getInfoAll($field = '', $where, $dbname, $sub_table, $order = 'id desc') {
    $data = Db::name($dbname)
            ->alias('a')
            ->field($field)
            ->join($sub_table . ' b', 'a.groupid = b.id', 'left')
            ->where($where)
            ->order($order)
            ->select();

    // echo $this->getLastSql();exit;

    return $data;
  }

  /**
   * 查询一条用户数据
   * @param unknown $field
   * @param unknown $where
   * @param unknown $dbname
   */
  public function getOne($field = '', $where, $dbname) {
    return Db::name($dbname)->field($field)->where($where)->find();
  }

  /**
   * 修改用户信息
   * @param unknown $where 条件
   * @param unknown $data  修改数据
   * @param unknown $dbname 表名
   */
  public function updateUserOne($where, $data, $dbname) {
    $res = Db::name($dbname)->where($where)->update($data);
    if (empty($res)) {
      writelog($this->user_data['uid'], $this->user_data['username'], '用户【' . $this->user_data['username'] . '】修改管理员信息失败【id=' . $where['id'] . '】', 0, $this->user_data['model'] . '_log');
    } else {
      writelog($this->user_data['uid'], $this->user_data['username'], '用户【' . $this->user_data['username'] . '】修改管理员信息成功【id=' . $where['id'] . '】', 1, $this->user_data['model'] . '_log');
    }
    return $res;
  }

  /**
   * 查询所有用户组
   * @param unknown $order 排序
   * @param unknown $dbname 表名
   *  @param unknown $where 条件
   */
  public function getAllAuthGroup($dbname, $where, $order = 'id desc') {
    $data = Db::name($dbname)->where($where)->whereOr(['id' => 1])->order($order)->select();
    //echo  Db::name($dbname)->getLastSql();
    return $data;
    //return Db::name($dbname)->where($where)->order($order)->select();
  }

  /**
   * 添加用户
   * @param unknown $data  添加数据
   * @param unknown $dbname 表名
   */
  public function InsertUser($data, $dbname) {
    $data['add_time'] = time(); //添加时间
    $id = Db::name($dbname)->insertGetId($data);
    if (empty($id)) {
      writelog($this->user_data['uid'], $this->user_data['username'], '用户【' . $this->user_data['username'] . '】添加管理员失败', 0, $this->user_data['model'] . '_log');
    } else {
      writelog($this->user_data['uid'], $this->user_data['username'], '用户【' . $this->user_data['username'] . '】添加管理员【' . $data['account'] . '】成功', 1, $this->user_data['model'] . '_log');
    }
    return $id;
  }

  /**
   * 查询行政中心用户信息
   * @param unknown $field 查询字段
   * @param unknown $where 条件
   * @param unknown $dbname 主表
   * @param unknown $sub_table 分表
   * @param unknown $order 排序
   * @return array();
   */
  public function getAdminUserInfoAll($field = '', $where, $dbname, $sub_table, $order = 'id desc') {
    $data = Db::name($dbname)
            ->alias('a')
            ->field($field)
            ->join($sub_table . ' b', 'a.groupid = b.id', 'left')
            ->join('administrative_info' . ' c', 'a.a_id = c.a_id', 'left')
            ->where($where)
            ->order($order)
            ->select();
    return $data;
  }

  /**
   * 查询行政中心单个用户信息
   * @param unknown $field 查询字段
   * @param unknown $where 条件
   * @param unknown $dbname 主表
   * @param unknown $sub_table 分表
   * @param unknown $order 排序
   * @return array();
   */
  public function getAdminUserInfoOne($field = '', $where, $dbname, $sub_table, $order = 'id desc') {
    $data = Db::name($dbname)
            ->alias('a')
            ->field($field)
            ->join($sub_table . ' b', 'a.groupid = b.id', 'left')
            ->join('administrative_info' . ' c', 'a.a_id = c.a_id', 'left')
            ->where($where)
            ->order($order)
            ->find();
    return $data;
  }

  /**
   * 查询区域合伙人用户信息
   * @param unknown $field 查询字段
   * @param unknown $where 条件
   * @param unknown $dbname 主表
   * @param unknown $sub_table 分表
   * @param unknown $order 排序
   * @return array();
   */
  public function getOperationUserInfoAll($field = '', $where, $dbname, $sub_table, $order = 'id desc') {
    $data = Db::name($dbname)
            ->alias('a')
            ->field($field)
            ->join($sub_table . ' b', 'a.groupid = b.id', 'left')
            ->join('operation_info' . ' c', 'a.o_id = c.o_id', 'left')
            ->where($where)
            ->order($order)
            ->select();
    return $data;
  }

  /**
   * 查询区域合伙人单个用户信息
   * @param unknown $field 查询字段
   * @param unknown $where 条件
   * @param unknown $dbname 主表
   * @param unknown $sub_table 分表
   * @param unknown $order 排序
   * @return array();
   */
  public function getOperationUserInfoOne($field = '', $where, $dbname, $sub_table, $order = 'id desc') {
    $data = Db::name($dbname)
            ->alias('a')
            ->field($field)
            ->join($sub_table . ' b', 'a.groupid = b.id', 'left')
            ->join('operation_info' . ' c', 'a.o_id = c.o_id', 'left')
            ->where($where)
            ->order($order)
            ->find();
    return $data;
  }

}
